﻿using CMS.Controls.TabPageTemplate;
using CMS.Infos;
using CMS.REST;
using CMS.Util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace CMS.Controls.DisplayTabPages.ChartTabPages
{
    public class ExportBusyRoomChartTabPage : ChartExportTabPage
    {
        private DateTimePicker DTP_StartDate;
        private DateTimePicker DTP_EndDate;
        private ComboBox CB_Building;
        private PrintDocument PD_Document;

        protected override string MainButtonName => "开始打印";

        protected override string ExportName => "导出开门统计表";

        protected override void StartExport()
        {
            if (CB_Building.SelectedIndex == -1)
            {
                MessageBox.Show(this, "必须选择合适的教学楼才能进行打印", "错误");
                return;
            }

            Utils.PrivateProfile.WriteString("ExportBusyRoom", "Building", (string)CB_Building.SelectedItem);

            _PrintDate = DTP_StartDate.Value;
            _EndDate = DTP_EndDate.Value;
            _PrintBuilding = CB_Building.SelectedItem.ToString();

            if (_PrintDate == _EndDate)
            {
                PD_Document.DocumentName = $"打印{_PrintDate.ToShortDateString()}";
            }
            else
            {
                PD_Document.DocumentName = $"打印{_PrintDate.ToShortDateString()}到{_EndDate.ToShortDateString()}";
            }

            PD_Document.Print();

        }

        protected override void _InitFormPanel(FormPanel fp)
        {
            PD_Document = new PrintDocument();
            PD_Document.PrintPage += PD_Document_PrintPage;

            DTP_StartDate = new MyDateTimePicker();
            fp.AddField("开始打印日期", DTP_StartDate);

            DTP_EndDate = new MyDateTimePicker();
            fp.AddField("结束打印日期", DTP_EndDate);

            CB_Building = new MyComboBox
            {
                DropDownStyle = ComboBoxStyle.DropDownList
            };

            RESTResponse response = RESTClient.HttpGet("apis/v1/building_names");
            if (response.IsSuccess)
            {
                CB_Building.Items.AddRange( response.Data.ToObject<string[]>());
            }
            CB_Building.SelectedItem = Utils.PrivateProfile.ReadString("ExportBusyRoom", "Building", null);

            fp.AddField("教学楼", CB_Building);


            DTP_StartDate.ValueChanged += (sender, e) =>
            {
                if (DTP_StartDate.Value > DTP_EndDate.Value)
                {
                    DTP_EndDate.Value = DTP_StartDate.Value;
                }
            };

            DTP_EndDate.ValueChanged += (sender, e) =>
            {
                if (DTP_StartDate.Value > DTP_EndDate.Value)
                {
                    DTP_StartDate.Value = DTP_EndDate.Value;
                }
            };

            Button Bn_SelectToday = new MyButton()
            {
                Text = "选择今日"
            };
            Bn_SelectToday.Click += (sender, e) =>
            {
                DTP_StartDate.Value = DTP_EndDate.Value = DateTime.Now.Date;
            };
            FormBase.ColumnDefinition[] columns = FP_Main.CloneColumnDefinition(false,true,false);
            columns[1].Padding.Top = columns[1].Padding.Bottom = 15;

            fp.AddField("快速填写", Bn_SelectToday, null, columns);

            Button Bn_SelectWeek = new MyButton()
            {
                Text = "选择明天开始七日",
            };
            Bn_SelectWeek.Click += (sender, e) =>
            {
                DTP_StartDate.Value = DateTime.Now.Date;
                DTP_EndDate.Value = DTP_StartDate.Value.AddDays(6);
            };
            fp.AddField("快速填写", Bn_SelectWeek, null, columns);

        }


        #region 打印相关
        private DateTime _PrintDate;
        private DateTime _EndDate;
        private string _PrintBuilding;


        private static readonly Brush BGBrush = new SolidBrush(Color.FromArgb(255, 240, 240, 240));
        private static readonly StringFormat TitleFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
        private static readonly Font TitleFont = new Font("微软雅黑", 25);
        private static readonly Font ContentFont = new Font("微软雅黑", 20);


        private void PD_Document_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rect = e.MarginBounds;

            string title = _PrintDate.ToShortDateString() + " 开门统计表";
            Rectangle titleRect = rect;
            titleRect.Height = 80;
            rect.Y += 80;
            rect.Height -= 80;

            g.DrawString(title, TitleFont, Brushes.Black, titleRect, TitleFormat);

            Size size = new Size(rect.Width / 4, rect.Height / 25);

            g.FillRectangle(BGBrush, rect.X, rect.Y, rect.Width, size.Height);
            Rectangle curRect = new Rectangle(rect.Location, size);
            g.DrawString("课室", ContentFont, Brushes.Black, curRect, TitleFormat);
            curRect.Offset(size.Width, 0);
            g.DrawString("上午", ContentFont, Brushes.Black, curRect, TitleFormat);
            curRect.Offset(size.Width, 0);
            g.DrawString("下午", ContentFont, Brushes.Black, curRect, TitleFormat);
            curRect.Offset(size.Width, 0);
            g.DrawString("晚上", ContentFont, Brushes.Black, curRect, TitleFormat);

            _Publisher.WriteLine("INFO", $"正在拉取:{_PrintDate.ToShortDateString()}@{_PrintBuilding}的课室占用信息");

            RESTResponse response = RESTClient.HttpGet("apis/v1/busy_room",
                new Dictionary<string, string>()
                {
                    {"date",_PrintDate.ToShortDateString().Replace('/', '-') },
                    {"building",_PrintBuilding }
                }
                );
            if (!response.IsSuccess)
            {
                _Publisher.WriteLine("ERROR", $"拉取{_PrintDate.ToShortDateString()}@{_PrintBuilding}的课室占用信息失败,原因:{response.ErrMsg}");
                e.Cancel = true;
                return;
            }

            BusyRoom[] result = response.Data.ToObject<BusyRoom[]>();

            for (int i = 1; i < 25; i++)
            {
                int y = rect.Y + size.Height * i;

                if (i % 2 == 0)
                {
                    g.FillRectangle(BGBrush, rect.X, y, rect.Width, size.Height);
                }

                if (result.Length >= i)
                {
                    curRect = new Rectangle(rect.X, y, size.Width, size.Height);

                    g.DrawString(result[i - 1].Name, ContentFont, Brushes.Black, curRect, TitleFormat);
                    curRect.Offset(size.Width, 0);

                    if (!string.IsNullOrEmpty(result[i - 1].Busy[0]))
                    {
                        g.DrawString(result[i - 1].Busy[0], ContentFont, Brushes.Black, curRect, TitleFormat);
                    }
                    curRect.Offset(size.Width, 0);

                    if (!string.IsNullOrEmpty(result[i - 1].Busy[1]))
                    {
                        g.DrawString(result[i - 1].Busy[1], ContentFont, Brushes.Black, curRect, TitleFormat);
                    }
                    curRect.Offset(size.Width, 0);

                    if (!string.IsNullOrEmpty(result[i - 1].Busy[2]))
                    {
                        g.DrawString(result[i - 1].Busy[2], ContentFont, Brushes.Black, curRect, TitleFormat);
                    }
                }
            }

            if (_PrintDate < _EndDate)
            {
                _PrintDate = _PrintDate.AddDays(1);
                e.HasMorePages = true;
            }
        }
        #endregion
    }
}
